package com.nice.demo.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

import com.nice.demo.R;
import com.nice.demo.test.bean.PathInfo;
import com.nice.demo.test.bean.PointInfo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * author : jc
 * date   : 2020/3/13
 * desc   :
 * version: 1.0
 */
public class MyHandWritingView extends FrameLayout implements IGeometryListener {
    private static final String TAG = "MyHandWritingView";
    public static final String PENSEC = "sec_e-pen";
    static final String STROKE_VERSION = "1.0";
    private static final int SCALE_X = 0;
    private static final int SCALE_Y = 1;
    private static final int SCALE_XY = 2;
    private int mScaleType;
    private PathInfo mPathInfo;
    private Paint mGeometryPaint;
    private Paint mEdgePaint;
    private DrawType mDrawType;
    private int mDragPointRadius;
    private int mDragPointDiameter;
    private int mImgBtnDiameter;
    private int mDragPointDiameter2;
    private int mLimitLeft;
    private int mLimitTop;
    private int mLimitRight;
    private int mLimitBottom;
    private int mGeometryRealWidth;
    private int mGeometryRealHeight;
    private int mWidth;
    private int mHeight;
    private MyHandWritingCoreView mHandWritingCoreView;
    private View mEditGeometryView;
    private LayoutParams mEditParams;
    private RelativeLayout mGeometryRl;
    private Path mGeometryPath;
    private List<MyHandWritingView.DragInfo> mDragInfoList;
    private MyHandWritingView.IGeometryViewListener mGeometryViewListener;

    public MyHandWritingView(Context context) {
        this(context, (AttributeSet) null);
    }

    public MyHandWritingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyHandWritingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mScaleType = -1;
        this.init(context);
    }

    public MyHandWritingView(Context context, int width, int height) {
        super(context);
        this.mScaleType = -1;
        this.mWidth = width == 0 ? 1 : width;
        this.mHeight = height == 0 ? 1 : height;
        this.mHandWritingCoreView = new MyHandWritingCoreView(context, width, height);
        LayoutParams layoutParams = new LayoutParams(this.mWidth, this.mHeight);
        this.addView(this.mHandWritingCoreView, layoutParams);
        this.initDimens();
        this.mHandWritingCoreView.setGeometryListener(this);
    }

    private void init(Context context) {
        this.mHandWritingCoreView = new MyHandWritingCoreView(context);
        LayoutParams layoutParams = new LayoutParams(-1, -1);
        this.addView(this.mHandWritingCoreView, layoutParams);
        this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                MyHandWritingView.this.mWidth = MyHandWritingView.this.getWidth();
                MyHandWritingView.this.mHeight = MyHandWritingView.this.getHeight();
                if (HandWritingViewHelper.DEBUG) {
                    XHLog.i("MyHandWritingView", "onGlobalLayout() >>> mWidth:" + MyHandWritingView.this.mWidth + ", mHeight:" + MyHandWritingView.this.mHeight, new Object[0]);
                }

                if (MyHandWritingView.this.mWidth > 0 && MyHandWritingView.this.mHeight > 0 && MyHandWritingView.this.getViewTreeObserver().isAlive()) {
                    MyHandWritingView.this.initDimens();
                    MyHandWritingView.this.mHandWritingCoreView.setGeometryListener(MyHandWritingView.this);
                    MyHandWritingView.this.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

            }
        });
    }

    private void initDimens() {
        this.mDragPointDiameter = this.getResources().getDimensionPixelSize(R.dimen.hw_drag_point_diameter);
        this.mImgBtnDiameter = this.getResources().getDimensionPixelSize(R.dimen.hw_drag_point_diameter);
        this.mDragPointRadius = this.mDragPointDiameter / 2;
        this.mDragPointDiameter2 = this.mDragPointDiameter + this.mDragPointDiameter;
        this.mLimitLeft = this.mDragPointRadius;
        this.mLimitTop = this.mImgBtnDiameter + this.mDragPointRadius;
        this.mLimitRight = this.mWidth - this.mDragPointRadius;
        this.mLimitBottom = this.mHeight - this.mDragPointRadius;
        if (HandWritingViewHelper.DEBUG) {
            XHLog.i("MyHandWritingView", "initDimens() >>> mDragPointDiameter:" + this.mDragPointDiameter + ", mImgBtnDiameter:" + this.mImgBtnDiameter + ", mLimitRight:" + this.mLimitRight + ", mLimitBottom:" + this.mLimitBottom, new Object[0]);
        }

    }

    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
            this.mWidth = this.getWidth();
            this.mHeight = this.getHeight();
            this.mLimitRight = this.mWidth - this.mDragPointRadius;
            this.mLimitBottom = this.mHeight - this.mDragPointRadius;
        }

        if (HandWritingViewHelper.DEBUG) {
            XHLog.i("MyHandWritingView", "onLayout() >>> changed:" + changed + ", isGeometryEditable:" + this.mHandWritingCoreView.isGeometryEditable() + ", width:" + this.mWidth + ", height:" + this.mHeight + ", [left:" + left + ", top:" + top + ", right:" + right + ", bottom:" + bottom + "]", new Object[0]);
        }

    }

    public void setLayoutParams(android.view.ViewGroup.LayoutParams params) {
        super.setLayoutParams(params);
        LayoutParams layoutParams = (LayoutParams) this.mHandWritingCoreView.getLayoutParams();
        if (layoutParams == null) {
            layoutParams = new LayoutParams(params.width, params.height);
        } else {
            layoutParams.width = params.width;
            layoutParams.height = params.height;
        }

        if (HandWritingViewHelper.DEBUG) {
            XHLog.i("MyHandWritingView", "setLayoutParams >>> width:" + params.width + ", height:" + params.height, new Object[0]);
        }

        this.mHandWritingCoreView.setLayoutParams(layoutParams);
    }

    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int toolType = ev.getToolType(0);
        if (this.mHandWritingCoreView != null && toolType == 2) {
            int x = (int) ev.getX();
            int y = (int) ev.getY();
            if (this.mHandWritingCoreView.isGeometryType()) {
                if (!this.mHandWritingCoreView.isRubber() && !this.mHandWritingCoreView.isGeometryEditable() && (x < this.mLimitLeft || y < this.mLimitTop || x > this.mLimitRight || y > this.mLimitBottom)) {
                    if (HandWritingViewHelper.DEBUG) {
                        XHLog.i("MyHandWritingView", "onInterceptTouchEvent() >>> invalid position  x : " + x + ", y : " + y, new Object[0]);
                    }

                    this.showInvalidateGeometryToast();
                    return true;
                }
            } else if (this.mHandWritingCoreView.isInvalid(x, y)) {
                return true;
            }
        }

        return super.onInterceptTouchEvent(ev);
    }

    public void setOnTouchListener(OnTouchListener l) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setOnTouchListener(l);
        }
    }

    public void onShowEditGeometry(PathInfo pathInfo, Paint geometryPaint, DrawType drawType) {
        if (pathInfo != null && pathInfo.pointsList != null && geometryPaint != null && drawType != null) {
            if (pathInfo.bottom + this.mDragPointRadius > this.mHeight) {
                if (this.mHandWritingCoreView != null) {
                    this.mHandWritingCoreView.onCancelEditView();
                }

                this.reset();
                this.showInvalidateGeometryToast();
            } else {
                if (this.mEdgePaint == null) {
                    this.mEdgePaint = new Paint(5);
                    this.mEdgePaint.setStyle(Paint.Style.STROKE);
                    this.mEdgePaint.setColor(Color.parseColor("#32b48c"));
                    PathEffect effect = new DashPathEffect(new float[]{5.0F, 15.0F, 5.0F, 15.0F}, 1.0F);
                    this.mEdgePaint.setPathEffect(effect);
                }

                this.mPathInfo = pathInfo;
                this.mGeometryPaint = geometryPaint;
                this.mDrawType = drawType;
                switch (drawType) {
                    //等腰
                    case ISOSCELESTRIANGLE:
                    case TRIANGLE:
                    case TRAPEZIUM:
                    case RHOMBUS://菱形
                    case PARALLELOGRAM://平行四边形
                        this.initEditTriangleOrTrapeziumPath();
                        break;
                    case RECTANGLE:
                        this.initEditRectanglePath();
                        break;
                    case OVAL:
                        this.initEditOvalPath();
                        break;
                    case COORDINATE:
                        this.initEditCoordinatePath();
                        break;
                    case NUMBER_AXIS:
                        this.initEditNumberAxisPath();
                        break;
                    default:
                        return;
                }

                this.initGeometryView();
            }
        }
    }

    public boolean isGeometryInvalid(DrawType drawType, int minX, int minY, int maxX, int maxY) {
        int disX = Math.abs(maxX - minX);
        int disY = Math.abs(maxY - minY);
        boolean isInvalid;
        switch (drawType) {
            //等腰
            case ISOSCELESTRIANGLE:
            case TRIANGLE:
            case RECTANGLE:
            case RHOMBUS://菱形
                isInvalid = disX < this.mDragPointDiameter || disY < this.mDragPointDiameter;
                break;
            case TRAPEZIUM:
            case PARALLELOGRAM://平行四边形
                int offsetX = (maxX - minX) / 4;
                isInvalid = Math.abs(maxX - offsetX - (minX + offsetX)) < this.mDragPointDiameter || disY < this.mDragPointDiameter;
                break;
            case OVAL:
                isInvalid = disX < this.mDragPointDiameter2 || disY < this.mDragPointDiameter2;
                break;
            case COORDINATE:
                isInvalid = disX < this.mDragPointDiameter && disY < this.mDragPointDiameter;
                break;
            case NUMBER_AXIS:
                isInvalid = disX < this.mDragPointDiameter;
                break;
            default:
                isInvalid = true;
        }

        return isInvalid;
    }

    public void handleEditableGeometry() {
        this.saveGeometryView();
    }

    public int getLimitLeft() {
        return this.mLimitLeft;
    }

    public int getLimitTop() {
        return this.mLimitTop;
    }

    public int getLimitRight() {
        return this.mLimitRight;
    }

    public int getLimitBottom() {
        return this.mLimitBottom;
    }

    public int getGeometryRealWidth() {
        return this.mGeometryRealWidth;
    }

    public int getGeometryRealHeight() {
        return this.mGeometryRealHeight;
    }

    public int getDragPointRadius() {
        return this.mDragPointRadius;
    }

    public void showInvalidateGeometryToast() {
        Toast.makeText(this.getContext(), "请不要超出书写区域!", Toast.LENGTH_SHORT).show();
    }

    /**
     * 初始化画完图形保存或者取消的页面
     */
    private void initGeometryView() {
        this.setWillNotDraw(false);
        if (this.mEditGeometryView != null) {
            this.removeView(this.mEditGeometryView);
        }

        this.mEditGeometryView = View.inflate(this.getContext(), R.layout.layout_floating_shape_view, (ViewGroup) null);
        ImageView cancelIv = (ImageView) this.mEditGeometryView.findViewById(R.id.cancel_iv);
        ImageView saveIv = (ImageView) this.mEditGeometryView.findViewById(R.id.save_iv);
        this.mGeometryRl = (RelativeLayout) this.mEditGeometryView.findViewById(R.id.geometry_rl);
        this.reviseParams();
        this.addView(this.mEditGeometryView, this.mEditParams);
        this.initGeometryDragPointView();
        cancelIv.setOnClickListener(this.getCancelClickListener());
        saveIv.setOnClickListener(this.getSaveClickListener());
        this.mEditGeometryView.setOnTouchListener(this.getGeometryTouchListener());
    }


    /**
     * 画图形的几个点
     */
    private void initGeometryDragPointView() {
        this.mDragInfoList = new ArrayList();
        Iterator var1 = this.mPathInfo.pointsList.iterator();

        while (var1.hasNext()) {
            PointInfo pointInfo = (PointInfo) var1.next();
            if (pointInfo.index >= 0) {
                MyHandWritingView.DragInfo dragInfo = new MyHandWritingView.DragInfo();
                dragInfo.index = pointInfo.index;
                this.mDragInfoList.add(dragInfo);
                ImageView dragPointView = (ImageView) View.inflate(this.getContext(), R.layout.item_drag_point_view, (ViewGroup) null);
                dragPointView.setTag(pointInfo.index);
                dragPointView.setOnTouchListener(this.getDragOnTouchListener());
                this.mGeometryRl.addView(dragPointView, this.initOrUpdateDragViewParams((android.widget.RelativeLayout.LayoutParams) null, pointInfo));
            }
        }

    }

    private android.widget.RelativeLayout.LayoutParams initOrUpdateDragViewParams(android.widget.RelativeLayout.LayoutParams params, @NonNull PointInfo pointInfo) {
        if (params == null) {
            params = new android.widget.RelativeLayout.LayoutParams(this.mDragPointDiameter, this.mDragPointDiameter);
        }

        if (this.mDrawType == DrawType.OVAL) {
            switch (pointInfo.index) {
                case 0:
                    params.setMargins(this.mGeometryRealWidth / 2, 0, 0, 0);
                    break;
                case 1:
                    params.setMargins(this.mGeometryRealWidth, this.mGeometryRealHeight / 2, 0, 0);
                    break;
                case 2:
                    params.setMargins(this.mGeometryRealWidth / 2, this.mGeometryRealHeight, 0, 0);
                    break;
                case 3:
                    params.setMargins(0, this.mGeometryRealHeight / 2, 0, 0);
            }
        } else {
                params.setMargins(pointInfo.x - this.mPathInfo.left, pointInfo.y - this.mPathInfo.top, 0, 0);
        }

        return params;
    }

    //三角或梯形，菱形
    private void initEditTriangleOrTrapeziumPath() {
        this.initGeometryPath();
        List<PointInfo> pointsList = this.mPathInfo.pointsList;
        int i = 0;

        for (int pointsListSize = pointsList.size(); i < pointsListSize; ++i) {
            PointInfo pointInfo = (PointInfo) pointsList.get(i);
            if (i == 0) {
                this.mGeometryPath.moveTo((float) pointInfo.x, (float) pointInfo.y);
            } else {
                this.mGeometryPath.lineTo((float) pointInfo.x, (float) pointInfo.y);
            }
        }

        this.mGeometryPath.close();
    }

    private void initEditRectanglePath() {
        this.initGeometryPath();
        RectF rectF = new RectF((float) this.mPathInfo.left, (float) this.mPathInfo.top, (float) this.mPathInfo.right, (float) this.mPathInfo.bottom);
        this.mGeometryPath.addRect(rectF, Path.Direction.CW);
    }

    private void initEditOvalPath() {
        this.initGeometryPath();
        RectF rectF = new RectF((float) this.mPathInfo.left, (float) this.mPathInfo.top, (float) this.mPathInfo.right, (float) this.mPathInfo.bottom);
        this.mGeometryPath.addOval(rectF, Path.Direction.CW);
    }

    private void initEditCoordinatePath() {
        this.initGeometryPath();
        List<PointInfo> pointsList = this.mPathInfo.pointsList;
        if (pointsList.size() == 5) {
            PointInfo originInfo = (PointInfo) pointsList.get(4);
            if (this.mHandWritingCoreView != null) {
                this.mHandWritingCoreView.obtainCoordinatePath(this.mGeometryPath, this.mPathInfo.left, this.mPathInfo.top, this.mPathInfo.right, this.mPathInfo.bottom, originInfo.x, originInfo.y, 10.0F, 4.0F);
            }
        }
    }

    private void initEditNumberAxisPath() {
        this.initGeometryPath();
        List<PointInfo> pointsList = this.mPathInfo.pointsList;
        if (pointsList.size() == 3) {
            PointInfo maxInfo = (PointInfo) pointsList.get(0);
            PointInfo minInfo = (PointInfo) pointsList.get(1);
            PointInfo originInfo = (PointInfo) pointsList.get(2);
            if (this.mHandWritingCoreView != null) {
                this.mHandWritingCoreView.obtainNumberAxisPath(this.mGeometryPath, minInfo.x, maxInfo.x, originInfo.x, originInfo.y, 10.0F, 4.0F);
            }
        }
    }

    private void initGeometryPath() {
        if (this.mGeometryPath == null) {
            this.mGeometryPath = new Path();
        } else {
            this.mGeometryPath.reset();
        }

    }

    private void reviseParams() {
        this.mGeometryRealWidth = this.mPathInfo.right - this.mPathInfo.left;
        this.mGeometryRealHeight = this.mPathInfo.bottom - this.mPathInfo.top;
        int geometryEditWidth = this.mGeometryRealWidth + this.mDragPointDiameter;
        int geometryEditHeight = this.mGeometryRealHeight + this.mDragPointDiameter;
        int height = geometryEditHeight + this.mImgBtnDiameter;
        int left = this.mPathInfo.left - this.mDragPointRadius;
        int top = this.mPathInfo.top - this.mDragPointRadius - this.mImgBtnDiameter;
        int right = this.mPathInfo.right + this.mDragPointRadius;
        int bottom = this.mPathInfo.bottom + this.mDragPointRadius;
        if (HandWritingViewHelper.DEBUG) {
            XHLog.i("MyHandWritingView", "reviseParams() >>> width:" + geometryEditWidth + ", height:" + height + ", left:" + left + ", top:" + top + ", right:" + right + ", bottom:" + bottom, new Object[0]);
        }

        if (this.mEditParams == null) {
            this.mEditParams = new LayoutParams(geometryEditWidth, height);
        } else {
            this.mEditParams.width = geometryEditWidth;
            this.mEditParams.height = height;
        }

        this.mEditParams.setMargins(left, top, 0, 0);
    }

    @NonNull
    private OnClickListener getCancelClickListener() {
        return new OnClickListener() {
            public void onClick(View v) {
                MyHandWritingView.this.cancelGeometryView();
            }
        };
    }

    @NonNull
    private OnClickListener getSaveClickListener() {
        return new OnClickListener() {
            public void onClick(View v) {
                MyHandWritingView.this.saveGeometryView();
            }
        };
    }

    private void reset() {
        if (this.mEditGeometryView != null) {
            this.removeView(this.mEditGeometryView);
            this.mEditGeometryView = null;
        }

        this.setWillNotDraw(true);
        this.mScaleType = -1;
        this.mDrawType = null;
        this.mPathInfo = null;
        this.mGeometryPath = null;
        this.mDragInfoList = null;
    }

    @NonNull
    private OnTouchListener getGeometryTouchListener() {
        return new OnTouchListener() {
            int downX;
            int downY;

            public boolean onTouch(View v, MotionEvent event) {
                int toolType = event.getToolType(0);
                switch (event.getActionMasked()) {
                    case 0:
                        this.downX = (int) event.getX();
                        this.downY = (int) event.getY();
                        if (HandWritingViewHelper.DEBUG) {
                            XHLog.i("MyHandWritingView", "按下时坐标x:" + this.downX + ",y:" + this.downY, new Object[0]);
                        }
                    case 1:
                    default:
                        break;
                    case 2:
                        int moveX = (int) event.getX();
                        int moveY = (int) event.getY();
                        int distanceX = moveX - this.downX;
                        int distanceY = moveY - this.downY;
                        MyHandWritingView.this.updateGeometryViewParams(distanceX, distanceY);
                }

                return true;
            }
        };
    }

    private void updateGeometryViewParams(int distanceX, int distanceY) {
        if (HandWritingViewHelper.DEBUG) {
            XHLog.i("MyHandWritingView", "updateGeometryViewParams() >>> distanceX_" + distanceX + ", distanceY_" + distanceY, new Object[0]);
        }

        if (distanceX != 0 || distanceY != 0) {
            int tempLeft = this.mPathInfo.left;
            int tempTop = this.mPathInfo.top;
            int tempRight = this.mPathInfo.right;
            int tempBottom = this.mPathInfo.bottom;
            tempLeft += distanceX;
            tempRight += distanceX;
            tempTop += distanceY;
            tempBottom += distanceY;
            if (distanceX < 0) {
                if (tempLeft < this.mLimitLeft) {
                    tempLeft = this.mLimitLeft;
                    tempRight = tempLeft + this.mGeometryRealWidth;
                    distanceX = tempLeft - this.mPathInfo.left;
                }
            } else if (tempRight > this.mLimitRight) {
                tempRight = this.mLimitRight;
                tempLeft = tempRight - this.mGeometryRealWidth;
                distanceX = tempRight - this.mPathInfo.right;
            }

            if (distanceY < 0) {
                if (tempTop < this.mLimitTop) {
                    tempTop = this.mLimitTop;
                    tempBottom = tempTop + this.mGeometryRealHeight;
                    distanceY = tempTop - this.mPathInfo.top;
                }
            } else if (tempBottom > this.mLimitBottom) {
                tempBottom = this.mLimitBottom;
                tempTop = tempBottom - this.mGeometryRealHeight;
                distanceY = tempBottom - this.mPathInfo.bottom;
            }

            PointInfo pointInfo;
            for (Iterator var7 = this.mPathInfo.pointsList.iterator(); var7.hasNext(); pointInfo.y += distanceY) {
                pointInfo = (PointInfo) var7.next();
                pointInfo.x += distanceX;
            }

            this.mPathInfo.left = tempLeft;
            this.mPathInfo.top = tempTop;
            this.mPathInfo.right = tempRight;
            this.mPathInfo.bottom = tempBottom;
            switch (this.mDrawType) {
                //等腰
                case ISOSCELESTRIANGLE:
                case TRIANGLE:
                case TRAPEZIUM:
                case RHOMBUS://菱形
                case PARALLELOGRAM://平行四边形
                    this.initEditTriangleOrTrapeziumPath();
                    break;
                case RECTANGLE:
                    this.initEditRectanglePath();
                    break;
                case OVAL:
                    this.initEditOvalPath();
                    break;
                case COORDINATE:
                    this.initEditCoordinatePath();
                    break;
                case NUMBER_AXIS:
                    this.initEditNumberAxisPath();
            }

            this.reviseParams();
            this.mEditGeometryView.setLayoutParams(this.mEditParams);
            if (this.mHandWritingCoreView != null) {
                this.mHandWritingCoreView.updateRange(tempLeft, tempTop, tempRight, tempBottom);
            }

            this.invalidate(tempLeft, tempTop, tempRight, tempBottom);
        }
    }

    @NonNull
    private OnTouchListener getDragOnTouchListener() {
        return new OnTouchListener() {
            private PointInfo mPointInfo;
            int downX;
            int downY;

            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                    case 0:
                        int index = (Integer) v.getTag();
                        if (HandWritingViewHelper.DEBUG) {
                            XHLog.i("MyHandWritingView", "getDragOnTouchListener() index >>> " + index, new Object[0]);
                        }

                        this.downX = (int) event.getX();
                        this.downY = (int) event.getY();
                        this.mPointInfo = MyHandWritingView.this.obtainDragPointInfo(index);
                        if (this.mPointInfo == null) {
                            return false;
                        }
                        break;
                    case 1:
                        this.downX = 0;
                        this.downY = 0;
                        this.mPointInfo = null;
                        break;
                    case 2:
                        int moveX = (int) event.getX();
                        int moveY = (int) event.getY();
                        int distanceX = moveX - this.downX;
                        int distanceY = moveY - this.downY;
                        MyHandWritingView.this.updateDragView(this.mPointInfo, distanceX, distanceY);
                }

                return true;
            }
        };
    }

    private void updateDragView(PointInfo pointInfo, int distanceX, int distanceY) {
        if (HandWritingViewHelper.DEBUG) {
            XHLog.i("MyHandWritingView", "updateDragView() >>> distanceX >> " + distanceX + "; distanceY >> " + distanceY, new Object[0]);
        }

        if (pointInfo != null && (distanceX != 0 || distanceY != 0)) {
            if (distanceX != 0 && distanceY == 0) {
                this.mScaleType = 0;
            }

            if (distanceY != 0 && distanceX == 0) {
                this.mScaleType = 1;
            }

            if (distanceX != 0 && distanceY != 0) {
                this.mScaleType = 2;
            }

            switch (this.mDrawType) {
                case ISOSCELESTRIANGLE:
                case TRIANGLE:
                    this.updateTriangleDragView(pointInfo, distanceX, distanceY);
                    break;
                case TRAPEZIUM:
                case RHOMBUS://菱形
                case PARALLELOGRAM://平行四边形
                    this.updateTrapeziumDragView(pointInfo, distanceX, distanceY);
                    break;
                case RECTANGLE:
                    this.updateRectangleDragView(pointInfo, distanceX, distanceY);
                    break;
                case OVAL:
                    this.updateOvalDragView(pointInfo, distanceX, distanceY);
                    break;
                case COORDINATE:
                    this.updateCoordinateDragView(pointInfo, distanceX, distanceY);
                    break;
                case NUMBER_AXIS:
                    this.updateNumberAxisDragView(pointInfo, distanceX);
            }

            this.invalidateRec();
        }
    }

    private void updateTriangleDragView(PointInfo pointInfo, int distanceX, int distanceY) {
        if (this.updateDragPointInfo(pointInfo, distanceX, distanceY)) {
            this.updatePathRangeInfo();
            this.reviseParams();
            this.mEditGeometryView.setLayoutParams(this.mEditParams);
            this.initEditTriangleOrTrapeziumPath();
            this.updateDragViewParams();
        }

    }

    private void updateRectangleDragView(PointInfo pointInfo, int distanceX, int distanceY) {
        boolean canNotMove = this.isDragViewCanNotMove(pointInfo, distanceX, distanceY, this.mDragPointDiameter);
        if (!canNotMove) {
            List<MyHandWritingView.DragInfo> dragInfoList = this.obtainRecDragInfoList(this.mScaleType, pointInfo.index, distanceX, distanceY);
            if (this.updateDragPointInfo(pointInfo, distanceX, distanceY)) {
                Iterator var6 = dragInfoList.iterator();

                while (var6.hasNext()) {
                    MyHandWritingView.DragInfo dragInfo = (MyHandWritingView.DragInfo) var6.next();
                    if (dragInfo.index != pointInfo.index) {
                        PointInfo info = (PointInfo) this.mPathInfo.pointsList.get(dragInfo.index);
                        this.updateDragPointInfo(info, dragInfo.distanceX, dragInfo.distanceY);
                    }
                }

                this.updatePathRangeInfo();
                this.reviseParams();
                this.mEditGeometryView.setLayoutParams(this.mEditParams);
                this.initEditRectanglePath();
                this.updateDragViewParams();
            }

        }
    }

    private void updateTrapeziumDragView(PointInfo pointInfo, int distanceX, int distanceY) {
        boolean canNotMove = this.isDragViewCanNotMove(pointInfo, distanceX, distanceY, this.mDragPointDiameter);
        if (!canNotMove) {
            List<MyHandWritingView.DragInfo> dragInfoList = this.obtainTrapDragInfoList(this.mScaleType, pointInfo.index, distanceX, distanceY);
            if (this.updateDragPointInfo(pointInfo, distanceX, distanceY)) {
                Iterator var6 = dragInfoList.iterator();

                while (var6.hasNext()) {
                    MyHandWritingView.DragInfo dragInfo = (MyHandWritingView.DragInfo) var6.next();
                    if (dragInfo.index != pointInfo.index) {
                        PointInfo info = (PointInfo) this.mPathInfo.pointsList.get(dragInfo.index);
                        this.updateDragPointInfo(info, dragInfo.distanceX, dragInfo.distanceY);
                    }
                }

                this.updatePathRangeInfo();
                this.reviseParams();
                this.mEditGeometryView.setLayoutParams(this.mEditParams);
                this.initEditTriangleOrTrapeziumPath();
                this.updateDragViewParams();
            }

        }
    }

    private void updateOvalDragView(PointInfo pointInfo, int distanceX, int distanceY) {
        int index = pointInfo.index;
        if (index == 0 || index == 2) {
            distanceX = 0;
            if (distanceY == 0) {
                return;
            }
        }

        if (index == 1 || index == 3) {
            distanceY = 0;
            if (distanceX == 0) {
                return;
            }
        }

        boolean canNotMove = this.isDragViewCanNotMove(pointInfo, distanceX, distanceY, this.mDragPointDiameter2);
        if (!canNotMove && this.updateDragPointInfo(pointInfo, distanceX, distanceY)) {
            int tempIndex = index + 1;
            if (index == this.mPathInfo.pointsList.size() - 1) {
                tempIndex = 0;
            }

            PointInfo otherPoint = (PointInfo) this.mPathInfo.pointsList.get(tempIndex);
            this.updateDragPointInfo(otherPoint, distanceX, distanceY);
            this.updatePathRangeInfo();
            this.reviseParams();
            this.mEditGeometryView.setLayoutParams(this.mEditParams);
            this.initEditOvalPath();
            this.updateDragViewParams();
        }

    }

    private void updateCoordinateDragView(PointInfo pointInfo, int distanceX, int distanceY) {
        switch (pointInfo.index) {
            case 0:
            case 2:
                distanceX = 0;
                break;
            case 1:
            case 3:
                distanceY = 0;
        }

        boolean canNotMove = this.isDragViewCanNotMove(pointInfo, distanceX, distanceY, this.mDragPointRadius);
        if (!canNotMove && this.updateDragPointInfo(pointInfo, distanceX, distanceY)) {
            switch (pointInfo.index) {
                case 0:
                    this.mPathInfo.top = pointInfo.y;
                    break;
                case 1:
                    this.mPathInfo.right = pointInfo.x;
                    break;
                case 2:
                    this.mPathInfo.bottom = pointInfo.y;
                    break;
                case 3:
                    this.mPathInfo.left = pointInfo.x;
            }

            if (this.mHandWritingCoreView != null) {
                this.mHandWritingCoreView.updateRange(this.mPathInfo.left, this.mPathInfo.top, this.mPathInfo.right, this.mPathInfo.bottom);
            }

            this.reviseParams();
            this.mEditGeometryView.setLayoutParams(this.mEditParams);
            this.initEditCoordinatePath();
            this.updateDragViewParams();
        }

    }

    private void updateNumberAxisDragView(PointInfo pointInfo, int distanceX) {
        boolean canNotMove = this.isDragViewCanNotMove(pointInfo, distanceX, 0, this.mDragPointRadius);
        if (!canNotMove && this.updateDragPointInfo(pointInfo, distanceX, 0)) {
            if (pointInfo.index == 1) {
                this.mPathInfo.right = pointInfo.x;
            } else if (pointInfo.index == 3) {
                this.mPathInfo.left = pointInfo.x;
            }

            if (this.mHandWritingCoreView != null) {
                this.mHandWritingCoreView.updateRange(this.mPathInfo.left, this.mPathInfo.top, this.mPathInfo.right, this.mPathInfo.bottom);
            }

            this.reviseParams();
            this.mEditGeometryView.setLayoutParams(this.mEditParams);
            this.initEditNumberAxisPath();
            this.updateDragViewParams();
        }

    }

    private void updateDragViewParams() {
        Iterator var1 = this.mPathInfo.pointsList.iterator();

        while (var1.hasNext()) {
            PointInfo info = (PointInfo) var1.next();
            View viewWithTag = this.mGeometryRl.findViewWithTag(info.index);
            if (viewWithTag != null) {
                android.widget.RelativeLayout.LayoutParams params = this.initOrUpdateDragViewParams((android.widget.RelativeLayout.LayoutParams) viewWithTag.getLayoutParams(), info);
                viewWithTag.setLayoutParams(params);
            }
        }

    }

    private void updatePathRangeInfo() {
        if (this.mPathInfo != null && this.mPathInfo.pointsList != null) {
            int minX = -1;
            int minY = -1;
            int maxX = -1;
            int maxY = -1;
            Iterator var5 = this.mPathInfo.pointsList.iterator();

            while (var5.hasNext()) {
                PointInfo pointInfo = (PointInfo) var5.next();
                if (minX != -1) {
                    minX = Math.min(minX, pointInfo.x);
                } else {
                    minX = pointInfo.x;
                }

                if (minY != -1) {
                    minY = Math.min(minY, pointInfo.y);
                } else {
                    minY = pointInfo.y;
                }

                if (maxX != -1) {
                    maxX = Math.max(maxX, pointInfo.x);
                } else {
                    maxX = pointInfo.x;
                }

                if (maxY != -1) {
                    maxY = Math.max(maxY, pointInfo.y);
                } else {
                    maxY = pointInfo.y;
                }
            }

            this.mPathInfo.left = minX;
            this.mPathInfo.top = minY;
            this.mPathInfo.right = maxX;
            this.mPathInfo.bottom = maxY;
            if (HandWritingViewHelper.DEBUG) {
                XHLog.i("MyHandWritingView", "updatePathRangeInfo() >>> left:" + minX + ", top:" + minY + ", right:" + maxX + ", bottom:" + maxY, new Object[0]);
            }

            if (this.mHandWritingCoreView != null) {
                this.mHandWritingCoreView.updateRange(minX, minY, maxX, maxY);
            }

        }
    }

    private boolean updateDragPointInfo(PointInfo pointInfo, int distanceX, int distanceY) {
        if ((distanceX != 0 || distanceY != 0) && pointInfo != null) {
            int tempX = pointInfo.x;
            tempX += distanceX;
            int tempY = pointInfo.y;
            tempY += distanceY;
            if (HandWritingViewHelper.DEBUG) {
                XHLog.i("MyHandWritingView", "updateDragPointInfo() >>> tempX : " + tempX + ",tempY : " + tempY, new Object[0]);
            }

            if (tempX < this.mLimitLeft) {
                tempX = this.mLimitLeft;
            }

            if (tempY < this.mLimitTop) {
                tempY = this.mLimitTop;
            }

            if (tempX > this.mLimitRight) {
                tempX = this.mLimitRight;
            }

            if (tempY > this.mLimitBottom) {
                tempY = this.mLimitBottom;
            }

            pointInfo.x = tempX;
            pointInfo.y = tempY;
            pointInfo.drawTime = System.currentTimeMillis();
            return true;
        } else {
            return false;
        }
    }

    private List<MyHandWritingView.DragInfo> obtainRecDragInfoList(int scaleType, int index, int distanceX, int distanceY) {
        MyHandWritingView.DragInfo dragInfo0 = (MyHandWritingView.DragInfo) this.mDragInfoList.get(0);
        MyHandWritingView.DragInfo dragInfo1 = (MyHandWritingView.DragInfo) this.mDragInfoList.get(1);
        MyHandWritingView.DragInfo dragInfo2 = (MyHandWritingView.DragInfo) this.mDragInfoList.get(2);
        MyHandWritingView.DragInfo dragInfo3 = (MyHandWritingView.DragInfo) this.mDragInfoList.get(3);
        switch (scaleType) {
            case 0:
                dragInfo0.distanceY = dragInfo1.distanceY = dragInfo2.distanceY = dragInfo3.distanceY = 0;
                if (index != 0 && index != 3) {
                    dragInfo1.distanceX = dragInfo2.distanceX = distanceX;
                    dragInfo0.distanceX = dragInfo3.distanceX = 0;
                } else {
                    dragInfo0.distanceX = dragInfo3.distanceX = distanceX;
                    dragInfo1.distanceX = dragInfo2.distanceX = 0;
                }
                break;
            case 1:
                dragInfo0.distanceX = dragInfo1.distanceX = dragInfo2.distanceX = dragInfo3.distanceX = 0;
                if (index != 0 && index != 1) {
                    dragInfo2.distanceY = dragInfo3.distanceY = distanceY;
                    dragInfo0.distanceY = dragInfo1.distanceY = 0;
                } else {
                    dragInfo0.distanceY = dragInfo1.distanceY = distanceY;
                    dragInfo2.distanceY = dragInfo3.distanceY = 0;
                }
                break;
            case 2:
                switch (index) {
                    case 0:
                        this.updateDragInfo(dragInfo0, distanceX, distanceY);
                        this.updateDragInfo(dragInfo1, 0, distanceY);
                        this.updateDragInfo(dragInfo3, distanceX, 0);
                        this.updateDragInfo(dragInfo2, 0, 0);
                        break;
                    case 1:
                        this.updateDragInfo(dragInfo1, distanceX, distanceY);
                        this.updateDragInfo(dragInfo0, 0, distanceY);
                        this.updateDragInfo(dragInfo2, distanceX, 0);
                        this.updateDragInfo(dragInfo3, 0, 0);
                        break;
                    case 2:
                        this.updateDragInfo(dragInfo2, distanceX, distanceY);
                        this.updateDragInfo(dragInfo3, 0, distanceY);
                        this.updateDragInfo(dragInfo1, distanceX, 0);
                        this.updateDragInfo(dragInfo0, 0, 0);
                        break;
                    case 3:
                        this.updateDragInfo(dragInfo3, distanceX, distanceY);
                        this.updateDragInfo(dragInfo2, 0, distanceY);
                        this.updateDragInfo(dragInfo0, distanceX, 0);
                        this.updateDragInfo(dragInfo1, 0, 0);
                }
        }

        return this.mDragInfoList;
    }

    private List<MyHandWritingView.DragInfo> obtainTrapDragInfoList(int scaleType, int index, int distanceX, int distanceY) {
        MyHandWritingView.DragInfo dragInfo0 = (MyHandWritingView.DragInfo) this.mDragInfoList.get(0);
        MyHandWritingView.DragInfo dragInfo1 = (MyHandWritingView.DragInfo) this.mDragInfoList.get(1);
        MyHandWritingView.DragInfo dragInfo2 = (MyHandWritingView.DragInfo) this.mDragInfoList.get(2);
        MyHandWritingView.DragInfo dragInfo3 = (MyHandWritingView.DragInfo) this.mDragInfoList.get(3);
        switch (scaleType) {
            case 0:
                dragInfo0.distanceY = dragInfo1.distanceY = dragInfo2.distanceY = dragInfo3.distanceY = 0;
                switch (index) {
                    case 0:
                        dragInfo0.distanceX = distanceX;
                        dragInfo1.distanceX = dragInfo2.distanceX = dragInfo3.distanceX = 0;
                        return this.mDragInfoList;
                    case 1:
                        dragInfo1.distanceX = distanceX;
                        dragInfo0.distanceX = dragInfo2.distanceX = dragInfo3.distanceX = 0;
                        return this.mDragInfoList;
                    case 2:
                        dragInfo2.distanceX = distanceX;
                        dragInfo0.distanceX = dragInfo1.distanceX = dragInfo3.distanceX = 0;
                        return this.mDragInfoList;
                    case 3:
                        dragInfo3.distanceX = distanceX;
                        dragInfo0.distanceX = dragInfo1.distanceX = dragInfo2.distanceX = 0;
                        return this.mDragInfoList;
                    default:
                        return this.mDragInfoList;
                }
            case 1:
                dragInfo0.distanceX = dragInfo1.distanceX = dragInfo2.distanceX = dragInfo3.distanceX = 0;
                if (index != 0 && index != 1) {
                    dragInfo2.distanceY = dragInfo3.distanceY = distanceY;
                    dragInfo0.distanceY = dragInfo1.distanceY = 0;
                } else {
                    dragInfo0.distanceY = dragInfo1.distanceY = distanceY;
                    dragInfo2.distanceY = dragInfo3.distanceY = 0;
                }
                break;
            case 2:
                switch (index) {
                    case 0:
                        this.updateDragInfo(dragInfo0, distanceX, distanceY);
                        this.updateDragInfo(dragInfo1, 0, distanceY);
                        this.updateDragInfo(dragInfo3, 0, 0);
                        this.updateDragInfo(dragInfo2, 0, 0);
                        break;
                    case 1:
                        this.updateDragInfo(dragInfo1, distanceX, distanceY);
                        this.updateDragInfo(dragInfo0, 0, distanceY);
                        this.updateDragInfo(dragInfo2, 0, 0);
                        this.updateDragInfo(dragInfo3, 0, 0);
                        break;
                    case 2:
                        this.updateDragInfo(dragInfo2, distanceX, distanceY);
                        this.updateDragInfo(dragInfo3, 0, distanceY);
                        this.updateDragInfo(dragInfo1, 0, 0);
                        this.updateDragInfo(dragInfo0, 0, 0);
                        break;
                    case 3:
                        this.updateDragInfo(dragInfo3, distanceX, distanceY);
                        this.updateDragInfo(dragInfo2, 0, distanceY);
                        this.updateDragInfo(dragInfo0, 0, 0);
                        this.updateDragInfo(dragInfo1, 0, 0);
                }
        }

        return this.mDragInfoList;
    }

    private void updateDragInfo(@NonNull MyHandWritingView.DragInfo dragInfo, int distanceX, int distanceY) {
        dragInfo.distanceX = distanceX;
        dragInfo.distanceY = distanceY;
    }

    private PointInfo obtainDragPointInfo(int index) {
        if (this.mPathInfo != null && this.mPathInfo.pointsList != null && this.mPathInfo.pointsList.size() != 0) {
            Iterator var2 = this.mPathInfo.pointsList.iterator();

            PointInfo pointInfo;
            do {
                if (!var2.hasNext()) {
                    return null;
                }

                pointInfo = (PointInfo) var2.next();
                pointInfo.drawTime = System.currentTimeMillis();
            } while (pointInfo.index != index);

            return pointInfo;
        } else {
            return null;
        }
    }

    private boolean isDragViewCanNotMove(@NonNull PointInfo pointInfo, int distanceX, int distanceY, int tempLimit) {
        int index = pointInfo.index;
        int size = this.mPathInfo.pointsList.size();
        boolean isAxis = this.mDrawType == DrawType.NUMBER_AXIS || this.mDrawType == DrawType.COORDINATE;
        int nonMoveIndex;
        if (isAxis) {
            nonMoveIndex = size - 1;
        } else {
            nonMoveIndex = index + 2;
            if (nonMoveIndex >= size) {
                nonMoveIndex -= size;
            }
        }

        int tempX = pointInfo.x + distanceX;
        int tempY = pointInfo.y + distanceY;
        PointInfo nonMovePoint = (PointInfo) this.mPathInfo.pointsList.get(nonMoveIndex);
        int nonMoveX;
        if (this.mDrawType == DrawType.TRAPEZIUM
                ||this.mDrawType==DrawType.RHOMBUS
                ||this.mDrawType==DrawType.PARALLELOGRAM) {//梯形或菱形、平行四边形
            switch (index) {
                case 0:
                    nonMoveX = ((PointInfo) this.mPathInfo.pointsList.get(1)).x;
                    break;
                case 1:
                    nonMoveX = ((PointInfo) this.mPathInfo.pointsList.get(0)).x;
                    break;
                case 2:
                    nonMoveX = ((PointInfo) this.mPathInfo.pointsList.get(3)).x;
                    break;
                case 3:
                    nonMoveX = ((PointInfo) this.mPathInfo.pointsList.get(2)).x;
                    break;
                default:
                    nonMoveX = nonMovePoint.x;
            }
        } else {
            nonMoveX = nonMovePoint.x;
        }

        int nonMoveY = nonMovePoint.y;
        boolean canNotMove;
        if (isAxis) {
            if (distanceX == 0) {
                canNotMove = Math.abs(nonMoveY - tempY) < tempLimit;
            } else {
                canNotMove = distanceY != 0 || Math.abs(nonMoveX - tempX) < tempLimit;
            }
        } else {
            canNotMove = Math.abs(nonMoveX - tempX) < tempLimit || Math.abs(nonMoveY - tempY) < tempLimit;
        }

        if (!canNotMove) {
            switch (index) {
                case 0:
                    if (isAxis) {
                        canNotMove = tempY > nonMoveY - tempLimit;
                    } else {
                        canNotMove = tempY > nonMoveY - tempLimit || tempX > nonMoveX - tempLimit;
                    }
                    break;
                case 1:
                    if (isAxis) {
                        canNotMove = tempX < nonMoveX + tempLimit;
                    } else {
                        canNotMove = tempX < nonMoveX + tempLimit || tempY > nonMoveY - tempLimit;
                    }
                    break;
                case 2:
                    if (isAxis) {
                        canNotMove = tempY < nonMoveY + tempLimit;
                    } else {
                        canNotMove = tempY < nonMoveY + tempLimit || tempX < nonMoveX + tempLimit;
                    }
                    break;
                case 3:
                    if (isAxis) {
                        canNotMove = tempX > nonMoveX - tempLimit;
                    } else {
                        canNotMove = tempX > nonMoveX - tempLimit || tempY < nonMoveY + tempLimit;
                    }
            }
        }

        return canNotMove;
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (HandWritingViewHelper.DEBUG) {
            XHLog.i("MyHandWritingView", "onDraw()", new Object[0]);
        }

        if (this.mGeometryPath != null && this.mGeometryPaint != null) {
            canvas.drawPath(this.mGeometryPath, this.mGeometryPaint);
        }

        if (this.mPathInfo != null && this.mEdgePaint != null) {
            canvas.drawRect((float) (this.mPathInfo.left - this.mDragPointRadius), (float) (this.mPathInfo.top - this.mDragPointRadius), (float) (this.mPathInfo.right + this.mDragPointRadius), (float) (this.mPathInfo.bottom + this.mDragPointRadius), this.mEdgePaint);
        }

    }

    private void invalidateRec() {
        if (this.mPathInfo != null) {
            this.invalidate(this.mPathInfo.left, this.mPathInfo.top, this.mPathInfo.right, this.mPathInfo.bottom);
        }
    }

    public void setGeometryPaintColor(@ColorInt int color) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setGeometryPaintColor(color);
        }
    }

    public void setGeometryPaintStyle(@NonNull Paint.Style style) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setGeometryPaintStyle(style);
        }
    }

    public void setAxisUnit(int axisUnit) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setAxisUnit(axisUnit);
        }
    }

    public void cancelGeometryView() {
        boolean isSuccess = false;
        if (this.mHandWritingCoreView != null) {
            isSuccess = this.mHandWritingCoreView.onCancelEditView();
        }

        if (HandWritingViewHelper.DEBUG) {
            XHLog.i("MyHandWritingView", "取消几何图形!!! isSuccess : " + isSuccess, new Object[0]);
        }

        this.reset();
        if (isSuccess && this.mGeometryViewListener != null) {
            this.mGeometryViewListener.onCancelGeometry(this);
        }

    }

    public void saveGeometryView() {
        boolean isSuccess = false;
        if (this.mHandWritingCoreView != null) {
            isSuccess = this.mHandWritingCoreView.onSaveEditView(this.mDrawType);
        }

        if (HandWritingViewHelper.DEBUG) {
            XHLog.i("MyHandWritingView", "保存几何图形!!! isSuccess : " + isSuccess, new Object[0]);
        }

        this.reset();
        if (isSuccess && this.mGeometryViewListener != null) {
            this.mGeometryViewListener.onSaveGeometry(this);
        }

    }

    public MyHandWritingView.IGeometryViewListener getGeometryViewListener() {
        return this.mGeometryViewListener;
    }

    public void setGeometryViewListener(MyHandWritingView.IGeometryViewListener geometryViewListener) {
        this.mGeometryViewListener = geometryViewListener;
    }

    public boolean isGeometryEditable() {
        return this.mHandWritingCoreView != null && this.mHandWritingCoreView.isGeometryEditable();
    }

    public void closeHandWrite() {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.closeHandWrite();
        }
    }

    public void openHandWrite() {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.openHandWrite();
        }
    }

    public void setToWriting() {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setToWriting();
        }
    }

    public void setToRubber() {
        if (this.mHandWritingCoreView != null) {
            if (this.mHandWritingCoreView.isGeometryEditable()) {
                this.saveGeometryView();
            }

            this.mHandWritingCoreView.setToRubber();
        }
    }

    public void restoreToImage(SparseArray<PathInfo> storkeList) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.restoreToImageArray(storkeList);
        }
    }

    public void clear() {
        if (this.mHandWritingCoreView != null) {
            if (this.mHandWritingCoreView.isGeometryEditable()) {
                this.cancelGeometryView();
            }

            this.mHandWritingCoreView.clear();
        }

    }

    public void loadBitmap(Bitmap bitmap) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.loadBitmap(bitmap);
        }
    }

    public void loadBitmap(byte[] data) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.loadBitmap(data);
        }
    }

    public String getEncodeBitmap() {
        return this.mHandWritingCoreView == null ? "" : this.mHandWritingCoreView.getEncodeBitmap();
    }

    public byte[] getBitmapBytes() {
        return this.mHandWritingCoreView == null ? null : this.mHandWritingCoreView.getBitmapBytes();
    }

    public void recycleBitmap() {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.recycleBitmap();
        }
    }

    public String getStrokes() {
        if (this.mHandWritingCoreView == null) {
            return "";
        } else {
            if (this.mHandWritingCoreView.isGeometryEditable()) {
                this.saveGeometryView();
            }

            return this.mHandWritingCoreView.getStrokes();
        }
    }

    public boolean isStrokeChange() {
        return this.mHandWritingCoreView != null && this.mHandWritingCoreView.isStrokeChange();
    }

    public void resetStrokeChange() {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.resetStrokeChange();
        }
    }

    public void setPenColor(@ColorInt int color) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setPenColor(color);
        }
    }

    public int getPenColor() {
        return this.mHandWritingCoreView == null ? -1 : this.mHandWritingCoreView.getPenColor();
    }

    public int getGeometryPenColor() {
        return this.mHandWritingCoreView == null ? -1 : this.mHandWritingCoreView.getGeometryPenColor();
    }

    public boolean setDrawType(DrawType type) {
        return this.mHandWritingCoreView != null && this.mHandWritingCoreView.setDrawType(type);
    }

    public DrawType getDrawType() {
        return this.mHandWritingCoreView == null ? null : this.mHandWritingCoreView.getDrawType();
    }

    public boolean isRubber() {
        return this.mHandWritingCoreView != null && this.mHandWritingCoreView.isRubber();
    }

    public boolean getCanDraw() {
        return this.mHandWritingCoreView != null && this.mHandWritingCoreView.getCanDraw();
    }

    public void setCanDraw(boolean canDraw) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setCanDraw(canDraw);
        }
    }

    public Bitmap getBitmap() {
        return this.mHandWritingCoreView == null ? null : this.mHandWritingCoreView.getBitmap();
    }

    public void setBitmap(Bitmap mBitmap) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setBitmap(mBitmap);
        }
    }

    public void setBitmap(Bitmap mBitmap, String stroke) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setBitmap(mBitmap, stroke);
        }
    }

    public void setDebug(boolean isDebug) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setDebug(isDebug);
        }
    }

    public void setMotionEventCallback(MotionEventCallback callback) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setMotionEventCallback(callback);
        }
    }

    public IHandWritingViewCache getHandWritingViewCache() {
        return this.mHandWritingCoreView == null ? null : this.mHandWritingCoreView.getHandWritingViewCache();
    }

    public View getActionDownView() {
        return this.mHandWritingCoreView == null ? null : this.mHandWritingCoreView.getActionDownView();
    }

    public void setActionDownView(View actionDownView) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setActionDownView(actionDownView);
        }
    }

    public int getmWidth() {
        return this.mHandWritingCoreView == null ? -1 : this.mHandWritingCoreView.getmWidth();
    }

    public int getmHeight() {
        return this.mHandWritingCoreView == null ? -1 : this.mHandWritingCoreView.getmHeight();
    }

    public void setHandWritingViewCache(IHandWritingViewCache handWritingViewCache) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setHandWritingViewCache(handWritingViewCache);
        }
    }

    public void setRecycleListener(MyHandWritingView.RecycleListener recycleListener) {
        if (this.mHandWritingCoreView != null) {
            this.mHandWritingCoreView.setRecycleListener(recycleListener);
        }
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Object view = this;

        while (true) {
            ViewParent viewParent = ((View) view).getParent();
            if (viewParent instanceof ListView) {
                ListView listView = (ListView) viewParent;
                this.setRecycleListener(listView);
                break;
            }

            if (!(viewParent instanceof View)) {
                break;
            }

            view = (View) viewParent;
        }

    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    private void setRecycleListener(ListView listView) {
        listView.setRecyclerListener(new AbsListView.RecyclerListener() {
            public void onMovedToScrapHeap(View view) {
                ViewGroup viewGroup = (ViewGroup) view;
                MyHandWritingView.callHandWritingRecycle(viewGroup);
            }
        });
    }

    public static void callHandWritingRecycle(ViewGroup viewGroup) {
        for (int i = 0; i < viewGroup.getChildCount(); ++i) {
            View child = viewGroup.getChildAt(i);
            if (child instanceof MyHandWritingCoreView) {
                MyHandWritingCoreView hcv = (MyHandWritingCoreView) child;
                hcv.cacheBitmap(hcv.getStrokes());
                if (hcv.isStrokeChange() && hcv.recycleListener != null) {
                    hcv.recycleListener.onRecycleListener();
                }
            } else if (child instanceof ViewGroup) {
                callHandWritingRecycle((ViewGroup) child);
            }
        }

    }

    public boolean pourIntoTouchEvent(MotionEvent event) {
        return this.mHandWritingCoreView != null && this.mHandWritingCoreView.pourIntoTouchEvent(event);
    }

    class DragInfo {
        int index;
        int distanceX;
        int distanceY;

        DragInfo() {
        }
    }

    public interface IGeometryViewListener {
        void onCancelGeometry(MyHandWritingView var1);

        void onSaveGeometry(MyHandWritingView var1);
    }

    public interface RecycleListener {
        void onRecycleListener();
    }
}
